package com.ericdm.callingandsmsstatistics;

import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
//This class is for monitor the sended SMS!

@SuppressLint("SimpleDateFormat")
public class SmsObserver extends ContentObserver
{
	private Context context;
	String time1 = "", time2 = "";
	boolean isNoSMS = false;
	private static final String[] SMS_PROJECTION = new String[] { "address", "person", "date", "type", "body", };
	
	public SmsObserver(Context context,Handler handler)
	{
		super(handler);
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	@Override
	public void onChange(boolean selfChange)
	{	
		// TODO Auto-generated method stub
		super.onChange(selfChange);
		Cursor cursor = context.getContentResolver().query(Uri.parse("content://sms/sent"), SMS_PROJECTION, " type=?", new String[] { "2" }, "date desc");
		if (cursor != null)
		{
			if (!cursor.moveToFirst())
			{
				isNoSMS = true;   //There are no SMS.
			}
			if (cursor.moveToFirst())
			{
				SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex("date"))));
				String phoneNumber = cursor.getString(cursor.getColumnIndex("address")).toString();
				String time = sfd.format(date);
				if (time1.equals(""))
				{
					time1 = time;
					time2 = time;;
				}
				time1 = time;
				if (time1.compareTo(time2) > 0)
				{
					time1 = "";
					time2 = "";
					DatabaseOperator databaseOperator = new DatabaseOperator();
					databaseOperator.insertData(DatabaseOperator.DATA_TYPE_SENDED_SMS, "1", phoneNumber, time.substring(0, 10), time);
					String queryStartDateString = databaseOperator.QueryTimeStrat();
					String queryEndDateString = databaseOperator.QueryTimeEnd();
					String currentDateString =  time.substring(0, 10);
					if (currentDateString.compareToIgnoreCase(queryStartDateString) >= 0 && currentDateString.compareToIgnoreCase(queryEndDateString) <= 0)
					{
						int sumTime = (int) (databaseOperator.GetSumSendSMS() + 1);
						databaseOperator.insertSumSendSms(sumTime);
						int phoneSumtime = (int) (databaseOperator.getPhoneNumberSendSms(phoneNumber) + 1);
						databaseOperator.setPhoneNumberSendSms(phoneNumber, phoneSumtime);
					}
					cursor.close();
					return;
				}
				if (isNoSMS)
				{	
					DatabaseOperator databaseOperator = new DatabaseOperator();
					databaseOperator.insertData(DatabaseOperator.DATA_TYPE_SENDED_SMS, "1", phoneNumber, time.substring(0, 10), time);
					isNoSMS = false;
					String queryStartDateString = databaseOperator.QueryTimeStrat();
					String queryEndDateString = databaseOperator.QueryTimeEnd();
					String currentDateString =  time.substring(0, 10);
					if (currentDateString.compareToIgnoreCase(queryStartDateString) >= 0 && currentDateString.compareToIgnoreCase(queryEndDateString) <= 0)
					{
						int sumTime = (int) (databaseOperator.GetSumSendSMS() + 1);
						databaseOperator.insertSumSendSms(sumTime);
						int phoneSumtime = (int) (databaseOperator.getPhoneNumberSendSms(phoneNumber) + 1);
						databaseOperator.setPhoneNumberSendSms(phoneNumber, phoneSumtime);
					}
					cursor.close();
					return;
				}
			}
		}
	}	
}
